#include "../include/myclient.h"

myclient::myclient():client()
{
    this->money = 0;
}

myclient::~myclient()
{
    //dtor
}
void myclient::myclientstart()
{
    if(clientinit() == 1)
    {
        cout<<"Init successful ."<<endl;
        clientgetparameters();
        if(clientcreatesocket() == 1)
        {
            cout<<"Create socket successful."<<endl;
            if(clientconnect() == 1)
            {
                cout<<"Connected server ."<<endl;
                myclientplaygame();
            }
            else
            {
                cout<<"Winsock init fail ..."<<endl;
                clientclose();
                cin.get();
                cin.get();
                exit(-3);
            }
        }
        else
        {
            cout<<"Create socket fail ..."<<endl;
            clientcleanup();
            cin.get();
            cin.get();
            exit(-2);
        }
    }
    else
    {
        cout<<"Winsock init fail ..."<<endl;
        clientcleanup();
        cin.get();
        cin.get();
        exit(-1);
    }
}
void myclient::myclientplaygame()
{

    int step;
    int choose,st;

    while(1)
    {
       //cout<<string(10,'\n');
       string ty("Server muon mua ");
        cout<<"\t\t\t\t\tYour money :"<<money<<endl;
        clientrecv();
        st = rec.datagettype();
        switch(st)
        {
        case -2:

            cout<<"Lua chon  khong hop le ."<<endl;
            cout<<"Ban chon bao thu so <0-49>: ";
            do{
                cin>>choose;
            }while(choose <0 || choose >49);
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
           /* clientrecv();
            cout<<"Question : "<<rec.datagetwhat(0)<<endl<<endl;
            cout<<"Answer 1 :"<<rec.datagetwhat(1)<<"\tAnswer 2 : "<<rec.datagetwhat(2)<<endl;
            cout<<"Answer 3 :"<<rec.datagetwhat(3)<<"\tAnswer 4 : "<<rec.datagetwhat(4)<<endl;
            cout<<"Your answer <1-4>: ";
            cin>>choose;
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();*/
            break;
        case 0:
            cout<<"Game ket thuc ."<<endl;
            cout<<"Giai thuong cua ban hien co : "<<money<<endl;
            cin.get();cin.get();
            clientclose();
            exit(0);
        case 1:
            step = rec.datagetx();

            do{
                cout<<"Ban chon bao thu so <0-49>: ";
                cin>>choose;
            }while(choose <0 || choose >49);
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
            break;
        case 2:
            cout<<"Question : "<<rec.datagetwhat(0)<<endl<<endl;
            cout<<"Answer 1 :"<<rec.datagetwhat(1)<<"\tAnswer 2 : "<<rec.datagetwhat(2)<<endl;
            cout<<"Answer 3 :"<<rec.datagetwhat(3)<<"\tAnswer 4 : "<<rec.datagetwhat(4)<<endl;
            cout<<"Your answer <1-4>: ";
            cin>>choose;
            sen.datasetvalue(2,choose,"","","","","",-1);
            clientsend();
            break;
        case 3:
            int type ;type = rec.datagetx();
            //gia ca
            int price ;
            price = rec.datagetansertrue();


            switch(type)
            {
                case 1:
                    ty += " bao thu";
                    break;
                case 2:
                    ty += " 2 bao thu ";
                    break;
                case 3:
                    ty += " bao thu va ngan phieu ";
                    break;
            }
            int choo;choo = -1;
            cout<<ty<<" voi gia : "<<price<<endl;
            while(choo != 0 && choo != 1)
            {
                    cout<<"Ban co muon ban voi gia nay khong <1-co,0-khong> ? :";
                    cin>>choo;
            }
            if(choo == 0)
            {
                int yourprice;
                cout<<"Gia ban muon ban : ";cin>>yourprice;
                sen.datasetvalue(4,choo,"","","","","",yourprice);
            }
            else
            {
                sen.datasetvalue(4,choo,"","","","","",-1);
            }
            clientsend();
            int exi;
            cout<<"Ban co muon tiep tuc tro choi hay khong <1-co,0-khong> ? ";
            cin>>exi;
            if(exi == 0)
            {
                sen.datasetvalue(0,-1,"","","","","",-1);
                clientsend();
            }
            else
            {
                sen.datasetvalue(-1,-1,"","","","","",-1);
                clientsend();
            }
            break;
        case 4:
            money = money + rec.datagetx();
            break;

        }
    }
}
